package com.sara.controller;


import com.sara.entity.Employee;
import com.sara.entity.Role;
import com.sara.service.EmployeeService;
import com.sara.service.PermissionService;
import com.sara.service.RoleService;
import com.sara.util.Constant;
import com.sara.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage/employee")
public class EmployeeController  {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RoleService roleService;


    @GetMapping
    public String listPage(@RequestParam(name = "p",defaultValue = "1",required = false)Integer pageNum,
                           String nameMobile,
                           Integer roleId,
                           Model model){

        Map<String,Object> params = new HashMap<>();
        params.put("nameMobile",nameMobile);
        params.put("roleId",roleId);

        Page<Employee> employeeList = employeeService.findByParams(pageNum,params);
        List<Role> roleList = roleService.findAllRole();
        model.addAttribute("employeeList", employeeList);
        model.addAttribute("roleList", roleList);
        for (Employee employee :  employeeList.getTList()){
            System.out.println(employee);
        }
        return "manage/employee/list";
    }


    @GetMapping("/add")
    public String addPage(Model model){
        List<Role> roleList  = roleService.findAllRole();
        model.addAttribute("roleList",roleList);
        return "manage/employee/add";
    }

    @PostMapping("/add")
    public String add(Employee employee, Integer[] roleIds, RedirectAttributes attributes){
        employeeService.save(employee,roleIds);
        attributes.addFlashAttribute(Constant.SEND_PAGE_MASSAGE_KEY, Constant.SEND_PAGE_MASSAGE_ADD_SECCESS);
        return "redirect:/manage/employee";
    }

    @GetMapping("/edit/{id:\\d+}")
    public String editPage(@PathVariable Integer id, Model model){
        List<Role> roleList = roleService.findAllRole();
        Employee employee = employeeService.findEmployeeById(id);
        model.addAttribute("roleList", roleList);
        model.addAttribute("employee", employee);
        return "manage/employee/edit";

    }


    @PostMapping("/edit")
    public String edit(Employee employee, Integer[] roleIds, RedirectAttributes attributes){
        employeeService.editEmployee(employee,roleIds);
        attributes.addFlashAttribute(Constant.SEND_PAGE_MASSAGE_KEY, Constant.SEND_PAGE_MASSAGE_ADD_SECCESS);
        return "redirect:/manage/employee";
    }


    @GetMapping("/delete/{id:\\d+}")
    public String delete(@PathVariable Integer id, RedirectAttributes attributes){
        employeeService.deleteEmployee(id);
        attributes.addFlashAttribute(Constant.SEND_PAGE_MASSAGE_KEY, Constant.SEND_PAGE_MASSAGE_DELETE_SECCESS);
        return "redirect:/manage/employee";
    }

    @GetMapping("/state/{id:\\d+}")
    public String state(@PathVariable Integer id, RedirectAttributes attributes){
        System.out.println("获取到的id是：" + id);
        employeeService.editEmployeeState(id);
        attributes.addFlashAttribute(Constant.SEND_PAGE_MASSAGE_KEY, Constant.SEND_PAGE_MASSAGE_CONTENT);
        return "redirect:/manage/employee";
    }

    
}
